import json

import itchat
from itchat.content import *

from utils import Mongo_DBHelper
from utils.Mongo_DBHelper import getGirdFsPipline

if __name__ == '__main__':
    msgRcorder = itchat.new_instance()
    msgRcorder.auto_login(hotReload=True, enableCmdQR=2, statusStorageDir='msgRcorder.pkl')

    # @msgRcorder.msg_register([TEXT,
    #                           MAP,
    #                           CARD,
    #                           NOTE,
    #                           SHARING,
    #                           PICTURE,
    #                           RECORDING,
    #                           ATTACHMENT,
    #                           VIDEO,
    #                           FRIENDS,
    #                           SYSTEM], isGroupChat=True)

    @msgRcorder.msg_register(TEXT, isGroupChat=True)
    def msg_record(msg):
        if isinstance(msg['Text'], function):
            buffer = msg['Text']()
            pipline = getGirdFsPipline()
            pipline.put(buffer, filename=msg['MsgId'])

        msg['Text'] = None
        msgObj = json.dumps(msg)
        collection = Mongo_DBHelper.getCollection()
        item = json.loads(msgObj)
        collection.insert(item)


    msgRcorder.run()
